package cn.yyh.user.service.impl;

import cn.yyh.basic.exception.MyException;
import cn.yyh.basic.utils.StrUtils;
import cn.yyh.user.constant.Constants;
import cn.yyh.user.mapper.UserMapper;
import cn.yyh.user.pojo.po.User;
import cn.yyh.user.service.IVerifyCodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;


import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author 杨永航
 * @version 1.0
 * @Title: VerifyCodeServiceImpl
 * @Package: cn.yyh.user.service.impl
 * @date 2021/6/30
 */
@Service
public class VerifyCodeServiceImpl implements IVerifyCodeService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void smsCode(Map<String, Object> map) {
        String phone = map.get("phone").toString();
        String type = map.get("type").toString();
        // 1.业务判断.看phone是否为空
        if (ObjectUtils.isEmpty(phone) || ObjectUtils.isEmpty(type)) {
            throw new MyException("手机号不能为空,请重新输入!");
        }
        // 2.业务判断.用phone去数据库找,看有没有重复的

        User user = userMapper.getByPhone(phone);

        if ("register".equals(type)) {
            if (user != null) {
                throw new MyException("手机号重复,请检查后重新输入!");
            }
            String key = Constants.BUSINESS_REGISTER_PREFIX+phone;
            sendVerifyCode(key, phone);
        }else if("binder".equals(type)) {
            if (user != null) {
                throw new MyException("手机号重复,请检查后重新输入!");
            }
            String key = Constants.BUSINESS_BINDER_PREFIX+phone;
            sendVerifyCode(key, phone);
        }

    }

    private void sendVerifyCode(String key,String phone) {
        // 3.用"业务描述:"+phone去redis找,看是否为空
        Object temp = redisTemplate.opsForValue().get(key);
        String code;
        String  verifyCode;
        // 3.1.如果不为空,取出时间戳和当前对比,看是否超过1分钟
        if(temp != null) {
            long time = Long.parseLong(temp.toString().split(":")[0]);
            // 3.1.1.如果没超过,则抛异常,时间未到
            if (System.currentTimeMillis() - time <= 60*1000) {
                throw new MyException("请勿重复获取验证码!");
            }else{
                // 3.1.2.如果超过,则用当前时间戳+原验证码重新拼接成字符串
                verifyCode = temp.toString().split(":")[1];
                code = System.currentTimeMillis() + ":" + verifyCode;
            }
        }else {
            // 3.2.如果为空
            // 3.2.1.生成当前时间戳和随机字符串,并拼接成字符串
            verifyCode = StrUtils.getComplexRandomString(6);
            code = System.currentTimeMillis() + ":" + verifyCode;
        }

        // 4.把k,v设置到redis中,并设置过期时间为3分钟
        redisTemplate.opsForValue().set(key,code,3, TimeUnit.MINUTES);
        // 5.调用工具类,传入phone和要发送的信息,并发送短信
        System.out.println("您的验证是：" + verifyCode + ";请在三分钟内使用！");
//        SmsUtil.send(phone,"您的验证是：" + verifyCode + ";请在三分钟内使用！");
    }
}
